package com.itheima.project;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description：void的返回方式：使用方法的参数requesr的转发和response重定向进行数据传输和页面跳转
 */
@Controller
@RequestMapping(value ="void-type")
public class VoidTypeController {

    @RequestMapping("request-forward")
    public void requestForward(HttpServletRequest request, HttpServletResponse response){
        request.setAttribute("content", "requestForward");
        try {
            request.getRequestDispatcher("/WEB-INF/views/succeed.jsp")
                    .forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("response-redirect")
    public void responseRedirect(HttpServletRequest request,HttpServletResponse response){
        try {
            response.sendRedirect("request-forward");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("response-return")
    public void responseReturn(HttpServletResponse response){
        response.setContentType("application/json;charset=utf-8");
        String flag = "{\"id\":1,\"name\":\"小明\"}";
        try {
            response.getWriter().write(flag);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
